var action = '';
function ajaxFileUpload()	{
		$("#loading").ajaxStart(function(){
			$(this).show();
		});
		$("#loading").ajaxComplete(function(){
    		$(this).hide();
		});
    $("#qifUploadError").ajaxStart(function(){
			$(this).hide();
		});
    $("#qifUploadLoading").ajaxStart(function(){
			$(this).show();
		});
		$("#jqUploader").ajaxStart(function(){
			$(this).hide();
		});
		$.ajaxFileUpload ({
				url:'index.php?action=uploadQifSubmit&ajax=1',
				secureuri:false,
				fileElementId:'fileToUpload',
				dataType: 'json',
				success: function (data, status) {
				  $("#qifUploadLoading").hide();
					if(typeof(data.error) != 'undefined') {
						if(data.error != '') {
							$("#qifUploadError").show();
							$("#qifUploadErrorText").text(data.error);
							$("#jqUploader").show();
						} else	{
							alert(data.msg);
						  }
					 }
					else {
            $("#qifUploadResultCount").show();
					  $("#qifUploadResult").show();
            $("#qifUploadResultCount").append(data.msg);
          }
        },
				error: function (data, status, e) {
				  alert(data.error+"\n"+status+"\n"+e);
					$("#qifUploadError").show();
					$("#qifUploadErrorText").text(e);
				}
			}
		)
  return false;
	}

$(document).ready(function(){
  // hide the loading div
  $('#loading').hide();
   
  // edit transaction link (submit the form)
  $("a[editItem]").livequery('click', function(event){
    event.preventDefault();
    // get the data
    action = 'editTransactionSubmit';
    id = $(this).attr('id');
    category = $('#category').val();
    comment = $('#comment').val();
    postData = 'ajax=1&action='+action+'&id='+id+'&comment='+comment+'&category='+category;
    // show the loading div
    $('#loading').show();
    // send the request
    $.ajax({
      type: "POST",
      url: "index.php",
      data: postData,
      cache: false,
      success: function(data, status){
        $('#loading').hide();
        alert(data);
        }
      });
    });
    
  // edit category link (submit the form)
  $("a[editCategory]").livequery('click', function(event){
    event.preventDefault();
    // get the data
    action = 'editCategoriesSubmit';
    id = $(this).attr('id');
    categoryName = $('#'+id+'_name').val();
    categoryColor = $('#'+id+'_color').val();
    categoryMaxAmountPerMonth = $('#'+id+'_MaxAmountPerMonth').val();
    postData = 'ajax=1&action='+action+'&id='+id+'&name='+categoryName+'&color='+categoryColor+'&maxAmountPerMonth='+categoryMaxAmountPerMonth;
    // show the loading div
    $('#loading').show();
    // send the request
    $.ajax({
      type: "POST",
      url: "index.php",
      data: postData,
      cache: false,
      success: function(data, status){
        $('#loading').hide();
        alert(data);
        }
      });
    });

  // edit keyword link (submit the form)
  $("a[editKeyword]").livequery('click', function(event){
    event.preventDefault();
    // get the data
    action = 'editKeywordsSubmit';
    id = $(this).attr('id');
    keyword = $('#'+id+'_keyword').val();
    category = $('#'+id+'_category').val();
    postData = 'ajax=1&action='+action+'&id='+id+'&keyword='+keyword+'&category='+category;
    // show the loading div
    $('#loading').show();
    // send the request
    $.ajax({
      type: "POST",
      url: "index.php",
      data: postData,
      cache: false,
      success: function(data, status){
        $('#loading').hide();
        alert(data);
        }
      });
    });

  // register all 'action' links
  $('a[action]').livequery('click',function(event){
    action = $(this).attr('action');
    event.preventDefault();
    // show the loading div
    $('#loading').show();
    path = 'index.php';
    $('#main').load(path, {'ajax':1, 'action': $(this).attr('action')},function(){
      $('#loading').hide("fast");
      });
    });

  // register all 'toggle' links
  $('a[toggle]').livequery('click',function(event){
    //$(this).toggle('fast');
    $('#edit-'+$(this).attr('toggle')).toggle("fast");
    $('#div-'+$(this).attr('toggle')).toggle("fast");
    });
    
  // search form submittion
  $("form#searchForm").livequery('submit', function(event){
    event.preventDefault();
    // show the loading div
    $('#loading').show();
    $.ajax({
      type: "GET",
      url: "index.php",
      data: "ajax=1&action=search&keyWord="+$("#keys").val(),
      cache: false,
      success: function(html){
        $("#main").empty();
        $("#main").prepend(html);
        $('#loading').hide("fast");
        }
      });
    });
  
  // date form submittion
  $("form#dateForm").livequery('submit', function(event){
    event.preventDefault();
    // show the loading div
    $('#loading').show();
    $.ajax({
      type: "GET",
      url: "index.php",
      data: "ajax=1&action=setDateRange_backAction="+action+"_range="+$("#range").val(),
      cache: false,
      success: function(html){
        $("#main").empty();
        $("#main").prepend(html);
        $('#loading').hide("fast");
        }
      });
    });

  // end of ready()
  });